import mqtt from 'mqtt';
import { env } from './env';

const connectUrl = `ws://${env.mqtt.host}:${env.mqtt.port}/mqtt`
export const mqttClient = mqtt.connect(connectUrl, {
  clientId: env.mqtt.clientId,
  username: env.mqtt.username,
  password: env.mqtt.password
});

mqttClient.on('connect', () => {
  console.log('✅ MQTT connected');
});

mqttClient.on('error', (err) => {
  console.error('❌ MQTT connection error:', err);
});
mqttClient.subscribe('im/notice/5');

mqttClient.on("message", (topic, message) => {
  console.log(`Received message: ${message.toString()} on topic: ${topic}`)
  mqttClient.publish('im/notice/5', 'hello world')
})
